package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.mapper.ClientMapper;
import com.ruan.pojo.Client;
import com.ruan.service.ClientService;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Service//不写这个的话， @Autowired  private UserService userService;用不了
public class ClientServiceImpl extends ServiceImpl<ClientMapper, Client> implements ClientService {
//    @Autowired
//    ClientService clientService;

    @Autowired
    private ClientMapper clientMapper;

    @Override
    public String getCnameById(Long clientId) {
        LambdaQueryWrapper<Client> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Client::getName).eq(Client::getId, clientId);
//        Client client = client.getOne(queryWrapper);
        Client clients = clientMapper.selectOne(queryWrapper);
        return clients.getName();
//        return client != null ? client.getCname() : null;
    }

    /**
     * 将客户信息导出为Excel
     * @throws Exception
     * @return
     */
    @Override
    public byte[] exportToExcel() {
        List<Client> clientList = clientMapper.selectList(null);
//        为在调用PoiUtils类的exportClientInfo方法时，该方法是非静态方法，
//        而你是通过类名直接调用的，需要先创建PoiUtils对象再调用该方法。
//        PoiUtils poiUtils = new PoiUtils();
//        poiUtils.exportClientInfo(clientList);

//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        XSSFWorkbook excel=new XSSFWorkbook(); //工作簿，允许您在Java应用程序中创建、读取和修改Excel文件
        //创建一个工作表对象
        XSSFSheet sheet = excel.createSheet("sheet1");


        String[] headers = {"姓名","手机号", "性别",
                "所属公司", "省", "市", "区","详细地址","状态"};

        XSSFRow row1 = sheet.createRow(0); // Create the first row for headers

        for (int i = 0; i < headers.length; i++) {
            row1.createCell(i).setCellValue(headers[i]); // Create cell for each header
        }

        for (int i = 0; i < clientList.size(); i++) {
            Client client = clientList.get(i);
            XSSFRow row2 = sheet.createRow(i+1);

            int SexStatus = client.getStatus();
            String SexStatusText = (SexStatus == 1) ? "男" : "女";

            int status = client.getStatus();
            String statusText = (status == 1) ? "合作中" : "未合作";

            if(client != null){
                String[] clientInfo = {
                        client.getName(),client.getPhone(),
                        SexStatusText,client.getCompanyName(),
                        client.getProvinceName(),client.getCityName(),
                        client.getDistrictName(),client.getDetail(),
                        statusText
                };

                for (int j = 0; j < clientInfo.length; j++) {
                    row2.createCell(j).setCellValue(clientInfo[j]);
                }
            }
        }

        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
        header.setContentDispositionFormData("attachment", "客户表.xlsx");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            excel.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            excel.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outputStream.toByteArray();

    }

}
